package com.chris.homecare.components.renderers
{
	import com.chris.homecare.utils.CommonUtils;
	
	import feathers.controls.GroupedList;
	import feathers.controls.ImageLoader;
	import feathers.controls.Label;
	import feathers.controls.LayoutGroup;
	import feathers.controls.renderers.IGroupedListItemRenderer;
	import feathers.core.FeathersControl;
	import feathers.layout.VerticalLayout;
	
	import flash.filesystem.File;
	import flash.geom.Point;
	
	import starling.display.Quad;
	import starling.events.Event;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	
	public class EmployeesListRenderer extends FeathersControl implements IGroupedListItemRenderer
	{
		protected var _data:Object;
		protected var _groupIndex:int = -1;
		protected var _itemIndex:int = -1;
		protected var _layoutIndex:int = -1;
		protected var _owner:GroupedList;
		protected var _isSelected:Boolean = false;
		
		protected var _touchPointID:int = -1;
		private static const HELPER_TOUCHES_VECTOR:Vector.<Touch> = new <Touch>[];
		private static const HELPER_POINT:Point = new Point();
		
		protected var _paddingTop:Number = 0;
		protected var _paddingRight:Number = 0;
		protected var _paddingBottom:Number = 0;
		protected var _paddingLeft:Number = 0;
		
		protected var _gap:Number = 0.5;
		
		protected var nameLabel:Label;
		protected var titleLabel:Label;
		protected var background:Quad;
		protected var bottomBorder:Quad;
		protected var icon:ImageLoader;
		private var imageFrame:ImageLoader;
		protected var rightAccessory:ImageLoader;

		private var nameLayoutGroup:LayoutGroup;
		
		public function EmployeesListRenderer()
		{
			super();
			this.addEventListener(TouchEvent.TOUCH, touchHandler);
		}
		
		public function get data():Object
		{
			return this._data;
		}
		
		public function set data(value:Object):void
		{
			if(this._data == value)
			{
				return;
			}
			this._data = value;
			this.invalidate(INVALIDATION_FLAG_DATA);
		}
		
		public function get groupIndex():int
		{
			return this._groupIndex;
		}
		
		public function set groupIndex(value:int):void
		{
			this._groupIndex = value;
		}
		
		public function get itemIndex():int
		{
			return this._itemIndex;
		}
		
		public function set itemIndex(value:int):void
		{
			this._itemIndex = value;
		}
		
		public function get layoutIndex():int
		{
			return this._layoutIndex;
		}
		
		public function set layoutIndex(value:int):void
		{
			this._layoutIndex = value;
		}
		
		public function get owner():GroupedList
		{
			return GroupedList(this._owner);
		}
		
		public function set owner(value:GroupedList):void
		{
			if(this._owner == value)
			{
				return;
			}
			
			if(this._owner)
			{
				this._owner.removeEventListener(Event.SCROLL, owner_scrollHandler);
			}
			
			this._owner = value;
			
			if(this._owner)
			{
				this._owner.addEventListener(Event.SCROLL, owner_scrollHandler);
			}
			
			this.invalidate(INVALIDATION_FLAG_DATA);
		}
		
		public function get isSelected():Boolean
		{
			return this._isSelected;
		}
		
		public function set isSelected(value:Boolean):void
		{
			if(this._isSelected == value)
			{
				return;
			}
			this._isSelected = value;
			this.invalidate(INVALIDATION_FLAG_SELECTED);
			this.dispatchEventWith(Event.CHANGE);
		}
		
		protected function owner_scrollHandler(event:Event):void
		{
			this._touchPointID = -1;
		}
		
		public function get paddingTop():Number
		{
			return this._paddingTop;
		}
		
		public function set paddingTop(value:Number):void
		{
			if(this._paddingTop == value)
			{
				return;
			}
			this._paddingTop = value;
			this.invalidate(INVALIDATION_FLAG_STYLES);
		}
		
		public function get paddingRight():Number
		{
			return this._paddingRight;
		}
		
		public function set paddingRight(value:Number):void
		{
			if(this._paddingRight == value)
			{
				return;
			}
			this._paddingRight = value;
			this.invalidate(INVALIDATION_FLAG_STYLES);
		}
		
		public function get paddingBottom():Number
		{
			return this._paddingBottom;
		}
		
		public function set paddingBottom(value:Number):void
		{
			if(this._paddingBottom == value)
			{
				return;
			}
			this._paddingBottom = value;
			this.invalidate(INVALIDATION_FLAG_STYLES);
		}
		
		public function get paddingLeft():Number
		{
			return this._paddingLeft;
		}
		
		public function set paddingLeft(value:Number):void
		{
			if(this._paddingLeft == value)
			{
				return;
			}
			this._paddingLeft = value;
			this.invalidate(INVALIDATION_FLAG_STYLES);
		}
		
		public function get gap():Number
		{
			return this._gap;
		}
		
		public function set gap(value:Number):void
		{
			if(this._gap == value)
			{
				return;
			}
			this._gap = value;
			this.invalidate(INVALIDATION_FLAG_STYLES);
		}
		
		override protected function initialize():void
		{
			height=140*CommonUtils.scaleValue;
			background = new Quad(20, 20, 0xffffff);
			addChild(this.background);
			
			bottomBorder = new Quad(10, 1, 0xe6e6e6);
			addChild(this.bottomBorder);
			
			nameLayoutGroup = new LayoutGroup();
			var nameLayout:VerticalLayout = new VerticalLayout();
			nameLayout.gap = 3;
			nameLayoutGroup.layout = nameLayout;
			addChild(nameLayoutGroup);
			
			this.nameLabel = new Label();
			nameLabel.nameList.add("employee-name-label");
			nameLayoutGroup.addChild(this.nameLabel);
			
			this.titleLabel = new Label();
			titleLabel.nameList.add("employee-title-label");
			nameLayoutGroup.addChild(this.titleLabel);
			
			if(this.icon)
			{
				this.icon.removeFromParent(true);
			}
			
			this.icon = new ImageLoader();
			this.addChild(this.icon);
			
			imageFrame = new ImageLoader();
			imageFrame.source = "assets/images/homecare/image-overlay.png";
			this.addChild(imageFrame);
			
			if(this.rightAccessory)
			{
				this.rightAccessory.removeFromParent(true);
			}
			
			this.rightAccessory = new ImageLoader();
			this.rightAccessory.source = "/assets/images/homecare/EmployeesListArrow.png"
			this.addChild(this.rightAccessory);
		}
		
		override protected function draw():void
		{
			const dataInvalid:Boolean = this.isInvalid(INVALIDATION_FLAG_DATA);
			const selectionInvalid:Boolean = this.isInvalid(INVALIDATION_FLAG_SELECTED);
			const stylesInvalid:Boolean = this.isInvalid(INVALIDATION_FLAG_STYLES);
			var sizeInvalid:Boolean = this.isInvalid(INVALIDATION_FLAG_SIZE);
			
			this.commitData();
			this.layout();
			
			super.draw();
		}
		
		
		protected function commitData():void
		{
			if(this._owner && this._data)
			{
				this.nameLabel.text = this._data.name;
				this.titleLabel.text = this._data.position;
				
				this.icon.source = File.applicationStorageDirectory.resolvePath(this._data.imageUrl).url;
			}
			
			else
			{
				this.nameLabel.text = "";
				this.icon.source = "";
			}
		}
		
		protected function layout():void
		{
			this.rightAccessory.x = this.width - 40;
			this.rightAccessory.y = (this.height - 20)/2;
			this.icon.width = this.icon.height = imageFrame.width = imageFrame.height = 100*CommonUtils.scaleValue;
			this.background.width = this.bottomBorder.width = this.explicitWidth;
			this.background.height = this.explicitHeight;
			this.background.x = this.background.y;
			
			this.icon.x = imageFrame.x = 15;
			this.icon.y = imageFrame.y = (this.height - this.icon.height)/2;
			
			this.nameLayoutGroup.x = this.icon.width + 50*CommonUtils.scaleValue;
			this.nameLayoutGroup.y = this.icon.y + 15*CommonUtils.scaleValue;
			
			this.bottomBorder.y = this.explicitHeight -1;
			if(this._isSelected)
			{
				this.background.color = 0xDDDDDD;
			}
			
			else this.background.color = 0xFFFFFF;
		}
		
		protected function touchHandler(event:TouchEvent):void
		{
			if(!this._isEnabled)
			{
				return;
			}
			
			const touches:Vector.<Touch> = event.getTouches(this, null, HELPER_TOUCHES_VECTOR);
			if(touches.length == 0)
			{
				//end of hover
				return;
			}
			if(this._touchPointID >= 0)
			{
				var touch:Touch;
				for each(var currentTouch:Touch in touches)
				{
					if(currentTouch.id == this._touchPointID)
					{
						touch = currentTouch;
						break;
					}
				}
				
				if(!touch)
				{
					//end of hover
					HELPER_TOUCHES_VECTOR.length = 0;
					return;
				}
				
				if(touch.phase == TouchPhase.ENDED)
				{
					this._touchPointID = -1;
					touch.getLocation(this, HELPER_POINT);
					var isInBounds:Boolean = this.hitTest(HELPER_POINT, true) != null;
					if(isInBounds)
					{
						if(!this._isSelected)
						{
							this.isSelected = true;
						}
						
						else this.isSelected = false;
					}
				}
			}
			else //if we get here, we don't have a saved touch ID yet
			{
				for each(touch in touches)
				{
					if(touch.phase == TouchPhase.BEGAN)
					{
						this._touchPointID = touch.id;
						break;
					}
				}
			}
			HELPER_TOUCHES_VECTOR.length = 0;
		}
	}
}